//
//  ExchangeViewCell.m
//  NiuBanJin
//
//  Created by Billy on 15/10/16.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "ExchangeViewCell.h"
#import "Ticket.h"
@interface ExchangeViewCell ()

@property (nonatomic, strong) UILabel *label_scorePrompt;
@property (nonatomic, strong) UILabel *label_rateAndPeroid; //利率和周期提示
@property (nonatomic, strong) UILabel *label_scoreShown;
@property (nonatomic, strong) UILabel *label_scoreExchange;
@property (nonatomic, strong) CATextLayer *textLayer_right;
 //右边显示立即兑换的label
@property (nonatomic, strong) UIImageView *imageView_score_center; //中间小分图片
@property (nonatomic, strong) UIImageView *imageView_score; //左边图片
@end

@implementation ExchangeViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setBackgroundColor:BASECOLOR];
        [self subViewsCreation];
    }
    
    return self;
}

#pragma mark - subviews creation

/** 整个cell的构成 : 左边白色部分，中间缺品，右边蓝色部分
 *
 *       1. 左边白色部分：包括真正需要的分数，图片，文字提示
 *       2. 右边只是一个蓝色的区域
 */

- (void) subViewsCreation {
    
    CALayer *layer_parent = [UsefulUtil layerCreationWithFrame:CGRectMake(MULTIPLYWIDTH(30.0/2), MULTIPLYHEIGHT(30.0/2), SCREEN_WIDTH - MULTIPLYWIDTH(60.0/2), MULTIPLYHEIGHT(150.0/2)) andContents:nil];
    [layer_parent setBackgroundColor:BASECOLOR.CGColor];
    [layer_parent setCornerRadius:5.0];
    
    [self.contentView.layer addSublayer:layer_parent];
    
    [layer_parent setMasksToBounds:YES];
    // 左layer
    CALayer *layer_left = [UsefulUtil layerCreationWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - MULTIPLYWIDTH(210.0/2), MULTIPLYHEIGHT(150.0/2)) andContents:nil];
    
    [layer_left setBackgroundColor:[UIColor whiteColor].CGColor];
    [layer_parent addSublayer:layer_left];
    
    // 右layer
    CATextLayer *textLayer_right = [UsefulUtil textLayerCreationWithFrame:CGRectMake(MULTIPLYWIDTH(540.0/2), 0, MULTIPLYWIDTH(150.0/2), MULTIPLYHEIGHT(150.0/2)) content:nil fontSize:17.0];
    
    self.textLayer_right = textLayer_right;
    
    [textLayer_right setForegroundColor:[UIColor whiteColor].CGColor];
    [textLayer_right setBackgroundColor:[UsefulUtil colorWithHexString:@"#4fa6fd"].CGColor];
    [layer_parent addSublayer:textLayer_right];
    
    UILabel *label_right = [UsefulUtil labelCreationWithFrame:CGRectMake(textLayer_right.frame.origin.x+MULTIPLYWIDTH(76.0/2), MULTIPLYHEIGHT(68.0/2), MULTIPLYWIDTH(76.0/2), MULTIPLYHEIGHT(76.0/2)) textColor:[UIColor whiteColor] fontName:[UIFont boldSystemFontOfSize:17.0] text:@"立即\n兑换"];
    
    [label_right setNumberOfLines:0];
    [self.contentView addSubview:label_right];
    
    //中间缺角
    
    CALayer *layer_circle_top = [UsefulUtil layerCreationWithFrame:CGRectMake(CGRectGetMaxX(layer_left.frame)- MULTIPLYWIDTH(24.0/4), MULTIPLYHEIGHT(-24.0/4), MULTIPLYWIDTH(24.0/2), MULTIPLYHEIGHT(24.0/2)) andContents:nil];
    
    [layer_circle_top setBackgroundColor:BASECOLOR.CGColor];
    [layer_circle_top setCornerRadius:MULTIPLYWIDTH(24.0/4)];
    [layer_parent addSublayer:layer_circle_top];
    
    CALayer *layer_circle_bottom = [UsefulUtil layerCreationWithFrame:CGRectMake(layer_circle_top.frame.origin.x, MULTIPLYHEIGHT((150.0/2 - 24.0/4 )), MULTIPLYWIDTH(24.0/2), MULTIPLYHEIGHT(24.0/2)) andContents:nil];
    
    [layer_circle_bottom setBackgroundColor:BASECOLOR.CGColor];
    [layer_circle_bottom setCornerRadius:MULTIPLYWIDTH(24.0/4)];
    [layer_parent addSublayer:layer_circle_bottom];
    
    // 左layer的sublaver
    
    UIImageView *imageView_score = [[UIImageView alloc] initWithFrame:CGRectMake(MULTIPLYWIDTH(30.0/2), MULTIPLYHEIGHT(38.0/2), MULTIPLYWIDTH(144.0/2), MULTIPLYHEIGHT(142.0/2))];
    
    [self.contentView addSubview: imageView_score];
    self.imageView_score = imageView_score;
    
    [imageView_score setImage:[UIImage imageNamed:@"scoreIconCircle"]];
    
    
    self.label_scoreExchange = [UsefulUtil labelCreationWithFrame:CGRectMake(0, MULTIPLYHEIGHT(50.0/2), MULTIPLYWIDTH(138.0/2), MULTIPLYHEIGHT(60.0/3)) textColor:[UsefulUtil colorWithHexString:@"#4fa6fd"] fontName:[UIFont systemFontOfSize:19.0] text:@"200000"];
    [self.label_scoreExchange setTextAlignment:NSTextAlignmentCenter];
    [imageView_score addSubview:self.label_scoreExchange];
    
    // 中间的分
    UIImageView *imageView_score_center = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView_score.frame) + MULTIPLYWIDTH(50.0/2), MULTIPLYHEIGHT(56.0/2), MULTIPLYWIDTH(28.0/2), MULTIPLYHEIGHT(28.0/2))];
    
    self.imageView_score_center = imageView_score_center;
    [self.contentView addSubview: imageView_score_center];
    
    [imageView_score_center setImage:[UIImage imageNamed:@"fen"]];
    
    self.label_scoreShown = [UsefulUtil labelCreationWithFrame:CGRectMake(CGRectGetMaxX(imageView_score_center.frame)+MULTIPLYWIDTH(10.0/2), MULTIPLYHEIGHT(51.0/2), MULTIPLYWIDTH(200.0/2), MULTIPLYHEIGHT(40.0/2)) textColor:[UsefulUtil colorWithHexString:@"#434343"] fontName:[UIFont boldSystemFontOfSize:19.0] text:@"200000积分"];
    
    [self.contentView addSubview:self.label_scoreShown];
    
    self.label_scorePrompt = [UsefulUtil labelCreationWithFrame:CGRectMake(imageView_score_center.frame.origin.x, CGRectGetMaxY(imageView_score_center.frame) + MULTIPLYHEIGHT(16.0/2), MULTIPLYWIDTH(300.0/2), MULTIPLYHEIGHT(40.0/2)) textColor:[UsefulUtil colorWithHexString:@"#434343"] fontName:[UIFont systemFontOfSize:11.0] text:@"兑换橙色加息券1张"];
    
    [self.contentView addSubview:self.label_scorePrompt];

    // 增加的利率颜色标
    self.label_rateAndPeroid = [UsefulUtil labelCreationWithFrame:CGRectMake(self.label_scorePrompt.frame.origin.x, CGRectGetMaxY(self.label_scorePrompt.frame)- MULTIPLYHEIGHT(8.0/2) , MULTIPLYWIDTH(300.0/2), MULTIPLYHEIGHT(40.0/2)) textColor:[UsefulUtil colorWithHexString:@"#434343"] fontName:[UIFont systemFontOfSize:11.0] text:@"利率加1%, 期限30天"];
    
    [self.contentView addSubview:self.label_rateAndPeroid];
}

- (void)layoutSubviews {
   
    // 按颜色 给文字赋值
    
    if (self.ticket.ticketType == TicketTypeYellow ) {
        
        [self.label_scorePrompt setText:self.ticket.ticket_brief];
        [self.imageView_score_center setImage:[UIImage imageNamed:@"fenYellow"]];
        [self.imageView_score setImage:[UIImage imageNamed:@"scoreIconCircleYellow"]];
        [self.label_scoreExchange setTextColor:[UsefulUtil colorWithHexString:@"#fda946"]];
        [self.textLayer_right  setBackgroundColor:[UsefulUtil colorWithHexString:@"#fda946"].CGColor];
    } else if (self.ticket.ticketType == TicketTypeBlue) {
        
        [self.label_scorePrompt setText:self.ticket.ticket_brief];
        [self.imageView_score_center setImage:[UIImage imageNamed:@"fen"]];
        [self.imageView_score setImage:[UIImage imageNamed:@"scoreIconCircle"]];
        [self.label_scoreExchange setTextColor:[UsefulUtil colorWithHexString:@"#57b3e0"]];
        [self.textLayer_right setBackgroundColor:[UsefulUtil colorWithHexString:@"#57b3e0"].CGColor];
    } else if (self.ticket.ticketType == TicketTypeOrange) { //这儿和最后的else今后如果颜色一样就可以只要一个
        
        [self.label_scorePrompt setText:self.ticket.ticket_brief];
        [self.imageView_score_center setImage:[UIImage imageNamed:@"fenOrange"]];
        [self.imageView_score setImage:[UIImage imageNamed:@"scoreIconCircleOrange"]];
        [self.label_scoreExchange setTextColor:[UsefulUtil colorWithHexString:@"#ff5a00"]];
        [self.textLayer_right setBackgroundColor:[UsefulUtil colorWithHexString:@"#ff5a00"].CGColor];
    }else { //预防第四种颜色
        
        [self.label_scorePrompt setText:self.ticket.ticket_brief];
        [self.imageView_score_center setImage:[UIImage imageNamed:@"fenOrange"]];
        [self.imageView_score setImage:[UIImage imageNamed:@"scoreIconCircleOrange"]];
        [self.label_scoreExchange setTextColor:[UsefulUtil colorWithHexString:@"#ff5a00"]];
        [self.textLayer_right setBackgroundColor:[UsefulUtil colorWithHexString:@"#ff5a00"].CGColor];
    }
    
    // assign reall value
    
    [self.label_scoreExchange setText:[NSString stringWithFormat:@"%@",self.ticket.ticket_exchangeScore]];
    [self.label_scoreShown setText:[NSString stringWithFormat:@"%@积分",self.ticket.ticket_exchangeScore]];

    // 增加的利率颜色标
    
    NSString *str_rate = [NSString stringWithFormat:@"%@", self.ticket.ticket_number_value] ;//加多少利率
    NSString *str_peroid = [NSString stringWithFormat:@"%@", self.ticket.ticket_number_period] ;//期限
    
    NSString *str_rate_peroid =  [NSString stringWithFormat:@"利率加%@%%, 期限%@天",str_rate,str_peroid];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str_rate_peroid];
    
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:REDCOLOR range:NSMakeRange(3, str_rate.length +1)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:REDCOLOR range:NSMakeRange(str_rate_peroid.length - str_peroid.length-1, str_peroid.length+1)];

    [self.label_rateAndPeroid setAttributedText:attrStr];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
